package com.kiyuumirai.moviesever.controller;

import com.kiyuumirai.moviesever.service.TicketService;
import com.kiyuumirai.utils.enums.CodeEnum;
import com.kiyuumirai.utils.exception.ExpectedException;
import com.kiyuumirai.utils.model.dto.TicketDTO;
import com.kiyuumirai.utils.model.vo.ResultVO;
import com.kiyuumirai.utils.util.ResultFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author MirAi
 * @description 票务控制器
 * @date 2020/11/30 17:00
 */
@RestController
public class TicketController {

    /** 票务业务接口 */
    @Resource
    private TicketService ticketService;

    /**
     * 根据电影名和数量购票
     *
     * @param ticketDTO 电影名和购票数量
     */
    @PostMapping("/tickets")
    public ResultVO buy(@RequestBody TicketDTO ticketDTO) {

        try {

            return ResultFactory.success(ticketService.buy(ticketDTO));

        } catch (Exception e) {
            if (e instanceof ExpectedException) {
                return ResultFactory.fail(((ExpectedException) e).getCodeEnum());
            }
            return ResultFactory.fail(CodeEnum.UNKNOWN_FAIL);
        }

    }
}
